<?php
// ----------------------------------------------------------------------
// pn-commerce Shopping Cart Solution for PostNuke
// http://pncommerce.de
// http://www.sourceforge.net/projects/pn-commerce/
//
// It is based on PostKart 0.701QA from
//			Destin LeBlanc [ postkart@zenflesh.com ]
// ----------------------------------------------------------------------
// Source file:  $Source: /cvsroot/pn-commerce/pn-commerce/pnprmltextapi.php,v $
//
// Last changes from:  $Author: landseer $
//
// Revision:           $Revision: 1.3 $
//
// Date of revision    $Date: 2005/03/23 12:09:44 $
//
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------

require_once("modules/pncommerce/common.php");

/**
 * This is just to show the name of this api in the scheme edit area
 * This is a necessary function, don't remove it!
 *
 * @return string
 */
function pncommerce_prmltextapi_info ($args)
{
  return _PNC_PRMLTEXTAPI;
}

/**
 * This function is called during the getItem procedure
 * It sets a field of $args[object] with the name $args['object']['property']['name'] to the value of the longtextfield field of the property field
 * This is a necessary function, don't remove it!
 * @param $args[object] array the object that needs to be altered
 * @param $args[property] the propertydata for us
 * @return array
 */
function pncommerce_prmltextapi_load ($args)
{
    if(!isset($args['object'][$args['property']['name']])) {
        $args['object']['dynamicfields'][] = $args['property']['name'];
    }
    // unserialize the data
    $languagedata = unserialize($args['property']['longtextfield']);
    // get the users language and see if this is defined in the languagedata
    // array, if not, use the systems default language instead.
    $thislang = pnUserGetLang();
    if(!array_key_exists($thislang, $languagedata)) {
        $thislang = pnConfigGetVar('language');
    }
    $args['object'][$args['property']['name']] = $languagedata[$thislang];;
    return $args['object'];
}


/**
 * This function returns the stuff from the property array for the given purpose
 * The api can decide which template to use. Even formparts are possible
 * see the processForm for details about this
 * @param $args[purpose] string the porpose for example itemview, cartview or something like that
 * @param $args[purposedata] array special data for this purpose, for example the old and new amount of the item in the cart or something like that
 * @param $args[object] array the object that needs to be altered
 * @param $args[property] the propertydata for us
 * @return array
 */
function pncommerce_prmltextapi_getVisualOutput ($args)
{
    // extract language data
    $languagedata = unserialize($args['property']['longtextfield']);

    $smarty =& new pnRender('pncommerce');
    $smarty->caching = false;

    switch($args[purpose])
    {
      case "showtemplate":
         $template=$args['purposedata'];
         break;
      case "objectedit":
         $template="objectedit";
        // get installed languages
        $languages = mltext_getLanguages();
        // check if all installed languages are present in the languagedata array
        // if not, add them. This is used in the objectedit
        foreach($languages as $lang) {
            if(!array_key_exists($lang, $languagedata)) {
                $languagedata[$lang] = '';
            }
        }
        $smarty->assign('languages', $languagedata);
         break;
      case "schemeedit":
         $template="schemeedit";
         break;
      //This is a senseless example
      case "cartview":
      case "invoiceview":
      default:
         $template="objectview";
    }
    $smarty->assign('params', $args);
    // when displaying data to the user when showing an item we are interested in
    // the text for the users language or the fallback if the users language
    // text is not defined
    // get the users language and see if this is defined in the languagedata
    // array, if not, use the systems default language instead.
    $thislang = pnUserGetLang();
    if(!array_key_exists($thislang, $languagedata)) {
        $thislang = pnConfigGetVar('language');
    }
    $smarty->assign('multilingualtext', $languagedata[$thislang]);
    return $smarty->fetch( "pncommerce_prmltextapi_".$template.".html" );
}


/**
 * This function processes a form if necessary (for dropdown fields for sizes etc.)
 * Data should be processed and the modified objectarray has to be returned.
 * all data that should be saved permanently has to be stored in the apropriate propertiesdata field
 * SchemeEdit has it's own process function!
 * -
 * @param $args[purpose] string the porpose for example itemview, cartview or something like that
 * @param $args[purpose data] array special data for this purpose, for example the old and new amount of the item in the cart or something like that
 * @param $args[object] array the object that needs to be altered
 * @param $args[property] the propertydata for us
 * @return array
 */
function pncommerce_prmltextapi_processForm ($args)
{
    $languages = mltext_getLanguages();
    //This is a senseless example
    switch($args['purpose'])
    {
        case "objectedit":
            $languagedata = array();
            foreach($languages as $lang) {
                $languagedata[$lang] = pnVarCleanFromInput("prmltextapi_" . $args['property']['propertydata_id'] . "_" . $args['property']['object_id'] . "_" . $lang);
            }
//pncdebug("ld", $languagedata);
            $args['object']['PropertiesData'][$args['property']['propertydata_id']]['longtextfield'] = serialize($languagedata);
            $args['object'][$args['property']['name']]=$args['property']['longtextfield'];
        default:
            //<sleep>zzZzZzz</sleep>
    }
//pncdebug("obj", $args['object'], true);
    return $args['object'];
}

/**
 * This function processes the scheme form and saves the data to the DB
 * @param $args[oldproperty] the old property
 */
function pncommerce_prmltextapi_processSchemeForm ($args)
{
    if (!pnModAPILoad('pncommerce', 'admin'))
    {
        return showerrorpage("unable to load pnadminapi", __FILE__, __LINE__);

    }
//We should check for changes here and save only if necessary
//can be improved later :-)
    if(pnVarCleanFromInput($args['oldproperty']['property_id']."_prmltextapi_delete") <> '1') {
        $prop=array('property_id'=>$args['oldproperty']['property_id'],
                  'scheme_id'=>$args['oldproperty']['scheme_id'],
                  'type'=>"prmltext");
        $prop['name']=pnVarCleanFromInput($args['oldproperty']['property_id']."_prmltextapi_name");

        $config=array();
        $config['defaultvalue']=pnVarCleanFromInput($args['oldproperty']['property_id']."_prmltextapi_defaultvalue");
        $config['hideifempty']=(pnVarCleanFromInput($args['oldproperty']['property_id']."_prmltextapi_hideifempty")==1) ? 1: 0;
        $prop['config']=$config;

        $hidden = pnVarCleanFromInput($args['oldproperty']['property_id']."_prmltextapi_hidden");
        $prop['hidden'] = ($hidden==1) ? 1 : 0;
        //$prop['sortorder']=pnVarCleanFromInput($args['oldproperty']['property_id']."_prmltextapi_sortorder");
        return pnModAPIFunc( 'pncommerce', 'admin', 'propertyStore', $prop);
    } else {
        return pnModAPIFunc( 'pncommerce', 'admin', 'propertyDelete', array('property_id' => $args['oldproperty']['property_id']));
    }
}

/**
 * This function returns a new property with defaultdata
 * @return array
 */
function pncommerce_prmltextapi_new ($args)
{
    $prop=array('property_id'=>-1,
                'scheme_id'=>-1,
                'type'=>"prmltext");
    $prop['name']="mlText";
    $prop['config']="";
    $prop['hidden']=0;
    $prop['sortorder'] = (int)$args['sortorder'];
    return $prop;
}

/**
 * This function returns a new propertydata block with defaultdata
 * @param $args['property'] array property
 * @return array
 */
function pncommerce_prmltextapi_newdata ($args)
{
    extract($args);
    unset($args);

    $langs = mltext_getLanguages();
    $languages = array();
    foreach($langs as $lang) {
        $languages[$lang] = "";
    }
    $property['longtextfield'] = serialize($languages);
    return $args;
}

/**
 * internal functions
 *
 */
function mltext_getLanguages()
{
    // check the language folders in /language
    $dir = opendir("language");
    $langs = array();
    while($lang=readdir($dir)) {
        if((strlen($lang)>2) && (is_dir("language/$lang")==true) && ($lang<>"CVS")) {
            array_push( $langs, $lang );
        }
    }
    return $langs;
}


?>
